package com.dd.ent.car.ui.widget;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.dd.ent.car.R;


/**
 * Created by aqu-spring on 16/7/12.
 * 视频基本的view
 */
public class VideoView extends FrameLayout implements View.OnClickListener {
    private SurfaceView surfaceView;
    private Context mContext;
    private FrameLayout rootView;
    private FrameLayout fraContent;
    private boolean isBegin;

    private ProgressBar progressBar;

    public VideoView(Context context) {
        super(context);
        init(context);
    }

    public VideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        View root = LayoutInflater.from(context).inflate(R.layout.vieo_view, this, true);
        surfaceView = (SurfaceView) root.findViewById(R.id.video_surface);
        rootView = (FrameLayout) root.findViewById(R.id.root_view);
        fraContent = (FrameLayout) root.findViewById(R.id.fra_share);
        progressBar = (ProgressBar) root.findViewById(R.id.pb_progress);
        rootView.setOnClickListener(this);
    }

    public SurfaceView getSurfaceView() {
        return surfaceView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.root_view:
                if (!isBegin) return;
                if (fraContent.getVisibility() == VISIBLE) {
                    fraContent.setVisibility(GONE);
                } else {
                    fraContent.setVisibility(VISIBLE);
                }
                break;

            default:
                break;
        }
    }

    /**
     * 停止播放
     */
    public void stop() {
        if (fraContent != null) {
            fraContent.setVisibility(GONE);
        }
        isBegin = false;
    }

    /**
     * 开始播放,设置开始播放标志
     *
     * @param isBegin
     */
    public void setIsBegin(boolean isBegin) {
        this.isBegin = isBegin;
    }

    /**
     * 加载播放链接出错
     *
     * @param mp
     * @param what
     * @param extra
     */

    public void mediaPlayerError(MediaPlayer mp, int what, int extra) {

        Toast.makeText(mContext, what + "+" + extra, Toast.LENGTH_SHORT).show();
    }

    public void setPrograss(int max, int current) {
        if (progressBar != null) {
            progressBar.setMax(max);
            progressBar.setProgress(current);
        }
    }

    public void resetData() {
        if (fraContent != null) {
            fraContent.setVisibility(GONE);
        }
    }

}
